import cgi
import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template

class Tipo(db.Model):
    nome = db.StringProperty(required=True)
    descricao = db.TextProperty()
    
class Artigo(db.Model):
    nome = db.StringProperty(required=True)
    tipo = db.ReferenceProperty(Tipo)
    date = db.DateTimeProperty(auto_now_add=True)
    imagem = db.BlobProperty()
    #m = MyModel()
    #m.blob = db.Blob(open("image.png").read())
    aroma = db.StringProperty()
    cor = db.StringProperty()
    altura = db.IntegerProperty()
    largura = db.IntegerProperty()
    profundidade = db.IntegerProperty()
    peso = db.IntegerProperty()
    descricao = db.TextProperty()
    autor = db.UserProperty()
    materiais = db.StringProperty(choices=set(["feltro", "cera", "madeira", "la"]))

#vela = Artigo(nome="Vela de gelo")
#vela.put()

class MainPage(webapp.RequestHandler):
  def get(self):
    artigos_query = Artigo.all().order('-date')
    artigos = artigos_query.fetch(10)

    template_values = {
      'artigos': artigos,
      }

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))



application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
